/** 保存到相册
 * tempFilePath: 图片本地路径
 */
const savePhoneAlbum = (tempFilePath)=>{
  uni.getSetting({
    success(res){
      if (!res.authSetting["scope.writePhotosAlbum"]){
        uni.authorize({
          scope: "scope.writePhotosAlbum",
          success() {
            uni.saveImageToPhotosAlbum({
              filePath: tempFilePath || "",
              success() {
                uni.showToast({
                  title: "保存成功",
                  icon: "success",
                  duration: 2000,
                });
              },
              fail() {
                uni.showToast({
                  title: "保存失败！",
                  icon: "none",
                  duration: 2000,
                });
              },
            });
          },
          fail() {
            uni.showModal({
              title: "温馨提示",
              content: "您已拒绝授权，是否去设置打开？",
              confirmText: "确认",
              cancelText: "取消",
              success: function (res) {
                if (res.confirm) {
                  uni.openSetting({
                    success: (res) => {
                      res.authSetting = {
                        "scope.writePhotosAlbum": true,
                      };
                    },
                  });
                } else {
                  console.log("用户点击取消");
                };
              },
            });
          },
        })
      } else {
        uni.saveImageToPhotosAlbum({
          filePath: tempFilePath || "",
          success() {
            uni.showToast({
              title: "保存成功",
              icon: "success",
              duration: 2000,
            });
          },
          fail() {
            uni.showToast({
              title: "保存失败！",
              icon: "none",
              duration: 2000,
            });
          },
        });
      };
    },
  });
};


module.exports = {
  savePhoneAlbum,
}
